﻿using System;
using System.Windows;
using System.Windows.Media;
using Assergs.Windows;

namespace BookvarWpf.Windows
{
    /// <summary>
    /// Base class for all ToolWindows in Bookvar.
    /// </summary>
    public class BookvarToolWindow : ToolWindow
    {
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            FrameworkElement maximizeButton = FindMaximizeButton(this);
            if (maximizeButton != null)
            {
                maximizeButton.Visibility = Visibility.Collapsed;
            }
        }

        private static FrameworkElement FindMaximizeButton(DependencyObject root)
        {
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(root); i++)
            {
                DependencyObject child = VisualTreeHelper.GetChild(root, i);
                FrameworkElement element = child as FrameworkElement;
                if ( element != null )
                {
                    if ( element.Name == "_MaximizeButton" )
                    {
                        return element;
                    }
                }

                element = FindMaximizeButton(child);

                if ( element != null)
                {
                    return element;
                }
            }

            return null;
        }
    }
}
